BASE_STATION = "/data/cym/auto_sci_pipeline/workstation"
import meta_service.ucentre
from meta_service.ucentre import AnyLancer,ExecuteSQL,AnyTag

# 保存原有的数据库
# ORI_GLOBAL_DB = meta_service.ucentre.GetGlobalDB()
# 更换全局数据库
meta_service.ucentre.ReInitGlobalDB("sqlite:///workstation/root.db")


from meta_service.ucentre import ARXIVTYPEID
def make_date_tag(paper: AnyLancer):
    if paper.get_id_type() ==  ARXIVTYPEID:
        tag = AnyTag(paper.get_entry_id())
        if tag.HasTag("year"):
            return tag
        entry_id = paper.get_entry_id()
        header = entry_id.split(".")[0]
        year = "unkown"
        month = "unkown"
        if len(header) == 4:
            year = "20" + str(header[:2])
            month = str(header[2:])
        tag.AddTag("year",year)
        tag.AddTag("month",month)
        return tag
    return None


def GetTripletHallu():
    from app.researcher import AppRun,Date,Today

    KEYRELAT= "(triplet OR triplets) AND (hallucinat OR hallucination OR hallucinate)"
    from datetime import datetime

    year = datetime.now().year
    month = datetime.now().month
    month -= 10
    if month < 1:
        month += 12
        year -= 1

    ready,notready = AppRun(
        query=f"{KEYRELAT} AND submittedDate:[{Date(year,month,1)} TO {Today()}]",
        max_results=1000
    )

    print("not-ready:",len(notready))
    print("ready:",len(ready))

    AnyTag.TagAll(ready,"triplet-hallu","1")

    # tag year month day
    entryids = AnyTag.GetEntryIdsOfTag("triplet-hallu")
    for entry_id in entryids:
        make_date_tag(AnyLancer(entry_id))




from .topic import topicsender
@topicsender
def ExtractTripletHallu():
    import json
    all = AnyTag.GetEntryIdsOfTag("triplet-hallu")
    # print(len(all))
    keep = {}
    for entry_id in all:
        tag = AnyTag(entry_id)
        month = tag.GetTag("month")
        year = tag.GetTag("year")
        key = f"{year}-{month}"
        if key in keep:
            keep[key] += 1
        else:
            keep[key] = 1
    keys = list(keep.keys())
    keys.sort()
    # # print(keys)
    # for key in keys:
    #     print(key," -> ",keep[key])
    topic = "extract.data.triplet-hallu"
    data = json.dumps(keep)
    return topic,data

